package com.yjy.controller;
// Tomcat 10+ 写法

import com.github.pagehelper.PageInfo;
import com.yjy.common.Result;
import com.yjy.entity.Category;
import com.yjy.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Resource
    CategoryService categoryService;

    @GetMapping("/selectAll")
    public Result selectAll(){
        List<Category> categorys = categoryService.selectAll(null);
        return Result.success(categorys);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Category category) {//@RequestBody 接受前端传来的json参数 get是接受不到json的 post delete put可以接受
        categoryService.add(category);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Category category) {//@RequestBody 接受前端传来的json参数 get是接受不到json的 post delete put可以接受
        categoryService.update(category);
        return Result.success();
    }

    @DeleteMapping ("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.success();
    }




    /**
     * 分页查询
     * pageNum: 当前的页码
     * pageSize: 每页的个数
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize,
                             Category category)
    {
                             //@RequestParam(required = false)String name){//也可以Category category
        PageInfo<Category> categoryPageInfo = categoryService.selectPage(pageNum, pageSize,category);
        return Result.success(categoryPageInfo); //返回的是分页的对象
    }

}
